package com.bw.aop;

import com.bw.domain.JWTUtils;
import com.bw.domain.SysUser;
import com.bw.service.SysUserService;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author bobo棒
 * @date 2024年04月15日 13:53
 */

@Slf4j
@Aspect
@Component
public class LogAop {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    HttpServletRequest request;

    @Around(value = "execution(* com.bw.controller.*.*(..))")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
        //当前用户名、方法名、访问的ip地址、时间
        String username="未知用户";
        String userId = JWTUtils.getUserId();
        if(null!=userId){
            SysUser user = sysUserService.getById(userId);
            username =user.getUsername();
        }
       //方法名称
        String methodName = pjp.getSignature().getName();
        //ip
        String ip = request.getRemoteAddr();
        Date date = new Date();

        log.info("用户:{},方法名:{},ip地址:{},时间:{}",username,methodName,ip,date);
        return pjp.proceed();
    }

}
